package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public ItemsListVO<Order> selectOrderList(Integer page, Integer limit, String sort, String order,
                                           Short[] orderStatusArray, String orderSn, Integer userId) {
        PageHelper.startPage(page,limit);

        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(sort + " " + order);
        OrderExample.Criteria criteria = orderExample.createCriteria();
        if (orderStatusArray != null) {
            List<Short> orderStatusList = new ArrayList<>(Arrays.asList(orderStatusArray));
            criteria.andOrderStatusIn(orderStatusList);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (orderSn != null) {
            criteria.andOrderSnLike("%" + orderSn + "%");
        }
        List<Order> orderList = orderMapper.selectByExample((orderExample));

        PageInfo pageInfo = new PageInfo(orderList);
        long total = pageInfo.getTotal();

        ItemsListVO<Order> orderListVO = new ItemsListVO<>();
        orderListVO.setItems(orderList);
        orderListVO.setTotal(total);
        return orderListVO;
    }

    @Override
    public OrderDetailVO selectOrderDetailById(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(order.getId());
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        OrderDetailVO orderDetailVO = new OrderDetailVO();
        orderDetailVO.setOrder(order);
        orderDetailVO.setOrderGoods(orderGoods);

        User1 user1 = userMapper.selectByPrimaryKey(order.getUserId());
        User user = new User();
        user.setAvatar(user1.getAvatar());
        user.setNickname(user1.getNickname());
        orderDetailVO.setUser(user);

        return orderDetailVO;
    }

    @Override
    public Boolean refundOrder(RefundBO refundBO) {
        int refundOrder = orderMapper.updateRefundOrder(refundBO.getOrderId(), refundBO.getRefundMoney());
        if (refundOrder == 1) {
            return true;
        }
        return false;
    }

    @Override
    public Integer shipOrder(ShipBO shipBO) {
        int shipOrder = orderMapper.updateShipOrder(shipBO);
        if (shipOrder == 1) {
            return shipBO.getOrderId();
        }
        return null;
    }
}
